package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.SerOrderInfo;
import com.server.llh.llhserverwork.bean.SerOrderInfoDetail;
import com.server.llh.llhserverwork.port.SerOrderInfoInterface;
import com.server.llh.llhserverwork.utils.ListViewHeightUtil;
import com.server.llh.llhserverwork.utils.TimeUtil;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.AddListView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ZTF on 2017-06-01.
 */

public class SerOrderInfoHolder extends BaseAdapter{
    Context mContext;
    List<SerOrderInfo> serOrderInfoList ;
    SerOrderInfoInterface holderOnclickInterface;
    public SerOrderInfoHolder(Context mContext,List<SerOrderInfo> serOrderInfoList) {
        this.mContext = mContext;
        this.serOrderInfoList=serOrderInfoList;
    }
    public  void registCallBack(SerOrderInfoInterface holderOnclickInterface){
        this.holderOnclickInterface=holderOnclickInterface;
    }

    @Override
    public int getCount() {
        if (serOrderInfoList==null&&serOrderInfoList.size()==0){
            return  0;
        }
        return serOrderInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return serOrderInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        SerOrderHolder serOrderHolder=null;
        if (v==null){
            v = LayoutInflater.from(mContext).inflate(
                    R.layout.llh_s_order_view, parent, false);
            serOrderHolder=new SerOrderHolder();
            serOrderHolder. orderNum= (TextView) v.findViewById(R.id.order_num);
            serOrderHolder. orderDownTime= (TextView) v.findViewById(R.id.order_down_time);
            serOrderHolder. orderPaystatus= (TextView) v.findViewById(R.id.order_paystatus);
            serOrderHolder.llhOrderServer= (AddListView) v.findViewById(R.id.llh_order_server);
            serOrderHolder. orderMoney= (TextView) v.findViewById(R.id.order_money);
            serOrderHolder. orderYouhui= (TextView) v.findViewById(R.id.order_youhui);
            serOrderHolder. orderPayMoney= (TextView) v.findViewById(R.id.order_pay_money);
            serOrderHolder. goPay= (TextView) v.findViewById(R.id.go_pay);
            serOrderHolder.goQuitOrder= (TextView) v.findViewById(R.id.go_quit_order);
            v.setTag(serOrderHolder);
        }else{
            serOrderHolder= (SerOrderHolder) v.getTag();
        }
      final   SerOrderInfo itemData=serOrderInfoList.get(position);
        serOrderHolder. orderNum.setText("订单编号："+itemData.getOrderCode());
        if (itemData.getOrderPersionTime()!=null){
            serOrderHolder. orderDownTime.setText("下单时间："+ TimeUtil.dateToStrLong(itemData.getOrderPersionTime()));

        }else{
            serOrderHolder. orderDownTime.setText("");

        }

        serOrderHolder.orderPaystatus.setText("");
        serOrderHolder.orderMoney.setText("订单金额：¥"+itemData.getAmountPayable()+"元");
        serOrderHolder.orderYouhui.setText("优惠：0 元");
        if (itemData.getPayStatus()==1){
            serOrderHolder. orderPayMoney.setText("实际支付 ¥"+itemData.getAmountPayable()+"元");
            serOrderHolder. goPay.setVisibility(View.INVISIBLE);
            serOrderHolder. goQuitOrder.setVisibility(View.INVISIBLE);
        }else  {
            serOrderHolder. orderPayMoney.setText("实际支付 ¥"+0+"元");
            serOrderHolder. goPay.setVisibility(View.INVISIBLE);
            serOrderHolder.goPay.setText("去支付");
            serOrderHolder.goPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemData!=null){
                        holderOnclickInterface.updateData(itemData);
                    }
                }
            });
            serOrderHolder. goQuitOrder.setVisibility(View.INVISIBLE);
            serOrderHolder. goQuitOrder.setText("取消订单");
            serOrderHolder. goQuitOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastTool.showText(mContext,"取消订单");
                    if (itemData!=null){
                        holderOnclickInterface.deleteData(itemData);
                    }
                }
            });
          }

        Log.d("SerOrder","SerOrderInfoDetailList=="+itemData.getSerOrderInfoDetailList().size());
        OrderInfosAdapter orderInfosAdapter=new OrderInfosAdapter(mContext,itemData.getSerOrderInfoDetailList());
        serOrderHolder.llhOrderServer.setAdapter(orderInfosAdapter);
        ListViewHeightUtil.setListViewHeightBasedOnChildren( serOrderHolder.llhOrderServer);

        return v;
    }
    class SerOrderHolder {
        TextView orderNum;
        TextView orderDownTime;
        TextView orderPaystatus;
        AddListView llhOrderServer;
        TextView orderMoney;
        TextView orderYouhui;
        TextView orderPayMoney;
        TextView goPay;
        TextView goQuitOrder;
    }

    public static class   OrderInfosAdapter extends BaseAdapter{
      Context context;
      List<SerOrderInfoDetail> serOrderInfoDetailList;
     public OrderInfosAdapter(Context context,List<SerOrderInfoDetail> serOrderInfoDetailList){
         this.context=context;
         this.serOrderInfoDetailList=serOrderInfoDetailList;
     }
      @Override
      public int getCount() {
          if (serOrderInfoDetailList==null&&serOrderInfoDetailList.size()==0){
              return 0;
          }
          return serOrderInfoDetailList.size();
      }

      @Override
      public Object getItem(int position) {
          return serOrderInfoDetailList.get(position);
      }

      @Override
      public long getItemId(int position) {
          return position;
      }

      @Override
      public View getView(int position, View v, ViewGroup parent) {
          OrderInfoHolder orderInfoHolder=null;
            if (v==null){
                orderInfoHolder=new OrderInfoHolder();
                v = LayoutInflater.from(context).inflate(
                        R.layout.llh_order_item, parent, false);
                orderInfoHolder.imageDevice= (ImageView) v.findViewById(R.id.imageDevice);
                orderInfoHolder.describeTxt= (TextView) v.findViewById(R.id.describeTxt);
                orderInfoHolder.priceTxt= (TextView) v.findViewById(R.id.priceTxt);
                orderInfoHolder. deviceType= (TextView) v.findViewById(R.id.device_type);
                orderInfoHolder. deviceNum= (TextView) v.findViewById(R.id.device_num);
                orderInfoHolder. deviceName= (TextView) v.findViewById(R.id.device_name);
                orderInfoHolder. depositTxt= (TextView) v.findViewById(R.id.depositTxt);
                v.setTag(orderInfoHolder);
            }else{
                orderInfoHolder = (OrderInfoHolder) v.getTag();
            }
          SerOrderInfoDetail itemData=serOrderInfoDetailList.get(position);
          Log.d("getPackageName","getPackageName=="+itemData.getPackageName());
          Picasso.with(context).load(Constants.SER_IMAGE_URL+itemData.getPictureUrl()).into(orderInfoHolder.imageDevice);
          orderInfoHolder.describeTxt.setText(""+itemData.getPackageName());
          orderInfoHolder.priceTxt.setText("");
          orderInfoHolder. deviceType.setText("订单号："+itemData.getOrderCode());
//          orderInfoHolder. deviceNum.setText(""+itemData.getSerNum()+"件");
          orderInfoHolder.deviceName.setText("价格："+itemData.getPrice()+"/元");
          orderInfoHolder. depositTxt.setText("共"+itemData.getSerNum()+"个");
          return v;
      }
      class OrderInfoHolder {
          ImageView imageDevice;
          TextView describeTxt;
          TextView priceTxt;
          TextView deviceType;
          TextView deviceNum;
          TextView deviceName;
          TextView depositTxt;
      }
  }
}
